package com.thunbu.thunbu_meeting_system.viewMdel

import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.thunbu.lib_common.base.CoroutineViewModel
import com.thunbu.thunbu_meeting_system.await
import com.thunbu.thunbu_meeting_system.bean.AssetModel
import com.thunbu.thunbu_meeting_system.bean.AssetPageModel
import com.thunbu.thunbu_meeting_system.bean.StockPageModel
import com.thunbu.thunbu_meeting_system.bean.TodoWapperModel
import com.thunbu.thunbu_meeting_system.launch
import com.thunbu.thunbu_meeting_system.service.ApiService
import com.thunbu.thunbu_meeting_system.service.MainNet
import com.thunbu.thunbu_meeting_system.ternary
import com.thunbu.thunbu_meeting_system.toRequestBody


/**
 * @Description:
 * @author: fancl
 * @Date: 2023/3/16 21:26
 */
class InventoryTaskListViewModel : CoroutineViewModel<Unit>() {
    val stockPage = MutableLiveData<StockPageModel>()
    val stockInfoPage = MutableLiveData<StockPageModel>()
    val assetPage = MutableLiveData<AssetPageModel>()
    val operateResult = MutableLiveData<Boolean>()
    val gs1Model = MutableLiveData<AssetModel>()
    val fridResult = MutableLiveData<MutableList<String?>>()

    override fun initModel() {

    }


    fun requestGs1Info(gs1: String,stockTaskCode: String?) {
        launch({
            val result = MainNet.server(ApiService::class.java).queryAssetByGs1(gs1,stockTaskCode?:"").await(true,true)
            result.data?.let {
                if(it.assetStatus ==0){
                    operate(it.assetStatus,1, mutableListOf(it.id),it.stockTaskCode,it.assetRemark?:"")
                }else if(it.assetStatus ==1){
                    gs1Model.value=result.data
                }
            }

        })
    }

    fun requestGuidInfo(guid: String,stockTaskCode: String?) {
        launch({
            val result = MainNet.server(ApiService::class.java).queryDetail(guid).await(true,true)
            result.data?.let { item->
                item.gs1?.let { gs1->
                    requestGs1Info(gs1,stockTaskCode)
                }

            }

        })
    }


    fun requestStockList(currentIndex: Int, mPage: Int, originalPage: Int) {
        launch({
            val map = mutableMapOf<String, Any?>(
                    "current" to mPage,
                    "pageSize" to originalPage)
            if(currentIndex != 0){
                map["stockStatus"] = currentIndex.toString() //1 未完成盘点; 2 已完成盘点
            }
            val result = MainNet.server(ApiService::class.java).queryStockList(map.toRequestBody()).await(true,true)
            stockPage.value = result.data
        })
    }

    fun requestStockInfo(stockTaskCode: String?) {
        launch({
            val map = mutableMapOf<String, Any?>(
                    "stockTaskCode" to stockTaskCode, )
            val result = MainNet.server(ApiService::class.java).queryStockList(map.toRequestBody()).await(true,true)
            stockInfoPage.value = result.data
        })
    }



    fun queryAssetList(stockTaskCode: String?, mAssetStatus: Int, zcType:String? = null,zcName: String? = null,brand: String? = null,model: String? = null,user: String? = null,department: String? = null,address: String? = null, mPage: Int, originalPage: Int) {
        launch({
            val map = mutableMapOf<String, Any?>(
                    "current" to mPage,
                    "pageSize" to originalPage,
                    "stockTaskCode" to stockTaskCode,
                    "assetStatus" to mAssetStatus
            )
            if(!zcType.isNullOrEmpty()){
                map["zichanflmc"] = zcType
            }
            if(!zcName.isNullOrEmpty()){
                map["ZICHANMC"] = zcName
            }
            if(!brand.isNullOrEmpty()){
                map["pinpai"] = brand
            }
            if(!model.isNullOrEmpty()){
                map["guigexh"] = model
            }
            if(!user.isNullOrEmpty()){
                map["shiyongrid"] = user
            }
            if(!department.isNullOrEmpty()){
                map["shiyongglbmid"] = department
            }
            if(!address.isNullOrEmpty()){
                map["cunfangddid"] = address
            }
            val result = MainNet.server(ApiService::class.java).queryTaskList(map.toRequestBody()).await(true,true)
            assetPage.value = result.data
        })
    }

    fun operate(originalStatus: Int, status: Int, ids: List<String?>, stockTaskCode: String?,remark:String) {
        launch({
            val map = mutableMapOf<String, Any?>(
                    "status" to status,
                    "originalStatus" to originalStatus,
                    "ids" to ids,
                    "stockTaskCode" to stockTaskCode,
                    "remark" to remark,
            )
            val result = MainNet.server(ApiService::class.java).operate(map.toRequestBody()).await(true,true)
            operateResult.value = true
        })
    }

    fun frid(list: List<String>,stockTaskCode: String?) {
        launch({
            val result = MainNet.server(ApiService::class.java).queryGs1List(list).await(true,true)
            result?.let {
                 it.data?.let {  list->
                     val ids = mutableListOf<String?>()
                     val myCompanyIds = mutableListOf<String?>()
                     list.forEach { gs1->
                         val result = MainNet.server(ApiService::class.java).queryAssetByGs1(gs1,stockTaskCode?:"").await(false,true)
                         result.data?.let {
                             myCompanyIds.add(it.id)
                             if(it.assetStatus ==0){
                                 ids.add(it.id)
//                                 operate(it.assetStatus,1, mutableListOf(it.id),it.stockTaskCode,it.assetRemark?:"")
                             }
                         }
                     }
                     ToastUtils.show(myCompanyIds.isNotEmpty().ternary("已扫描${myCompanyIds.size}条","无本单位资产"))
                     if(ids.isNotEmpty()){
                         operate(0,1, ids,stockTaskCode,"")
                     }

                 }
            }
        })
    }

    fun deleteSurplus(ids: List<String?>) {
        launch({
            val result = MainNet.server(ApiService::class.java).deleteDiskSurplus(ids).await(true,true)
            operateResult.value = true
        })


    }

}